package com.kxn.multi.module.service.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.kxn.multi.module.service.base.BaseEntity;

import java.util.Collections;

/**
 * mybatis-plus 代码生成
 *
 * 文档：https://baomidou.com/pages/981406/#%E6%95%B0%E6%8D%AE%E5%BA%93%E9%85%8D%E7%BD%AE-datasourceconfig
 */
public class MysqlGenerator {

    //项目目录
    public static final String PROJECT_PATH = "/Users/kangxuening/IdeaProjects/multi-module/multi-module-service";

    //包名
    public static final String PACKAGE_NAME = "com.kxn.multi.module";

    //模块名
    public static final String MODULE_NAME = "com/kxn/multi/module/mq/service";


    public static void generator() {
        String url = "jdbc:mysql://localhost:3306/multi-module?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "";

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("kxn") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
//                            .fileOverride() // 覆盖已生成文件
                            .outputDir(PROJECT_PATH + "/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(PACKAGE_NAME) // 设置父包名
                            .moduleName(MODULE_NAME)  // 设置父包模块名
                            .controller("controller") //controller目录
                            .entity("entity")       //entity目录
                            .service("com/kxn/multi/module/mq/service")     //service目录
                            .serviceImpl("service.impl")
                            .mapper("mapper")       //mapper目录
                            .xml("mapper.xml")
                            .other("dto")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, PROJECT_PATH + "/src/main/resources/mapper/")); // 设置mapperXml生成路径
                })
                .templateConfig(builder -> {
                    //模板配置，模板在 resources/template/文件夹下，会覆盖原有模板
                    builder.controller("template/controller.java")
                            .service("template/service.java")
                            .serviceImpl("template/serviceImpl.java");
                })
                .strategyConfig(builder -> {
                    //entity策略配置
                    builder.entityBuilder()
                            .superClass(BaseEntity.class) //父类
                            .enableLombok()
                            .enableRemoveIsPrefix() //开启 Boolean 类型字段移除 is 前缀
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .addSuperEntityColumns("id", "created_at", "updated_at")
                            .addTableFills(new Column("created_at", FieldFill.INSERT))
                            .addTableFills(new Property("updated_at", FieldFill.INSERT_UPDATE))
                            .idType(IdType.AUTO)
                            .formatFileName("%s");

                    //mapper策略配置
                    builder.mapperBuilder()
                            .enableBaseResultMap() //启用 BaseResultMap 生成
                            .enableBaseColumnList() //启用BaseColumnList
                            .formatMapperFileName("%sMapper") //Mapper名称策略，如表user，生成UserMapper
                            .formatXmlFileName("%sMapper"); //mapper xml名称策略, 如表user，生成UserMapper.xml

                    //service策略配置
                    builder.serviceBuilder()
                            .formatServiceFileName("%sService") //设置service的命名策略,没有这个配置的话，生成的service和serviceImpl类前面会有一个I，比如IUserService和IUserServiceImpl
                            .formatServiceImplFileName("%sServiceImpl"); //设置serviceImpl的命名策略

                    //控制器策略配置
                    builder.controllerBuilder()
//                            .superClass(BaseController.class) //设置父类
//                            .enableHyphenStyle() //开启驼峰转连字符
                            .enableRestStyle() //开启生成@RestController 控制器
                            .formatFileName("%sController")
                            .build();

                    //待生成的表
                    builder.addTablePrefix("t_", "tbl_"); // 设置过滤表前缀
//                    builder.addInclude("user");
                    builder.addInclude("admin_user");
                    builder.addInclude("admin_role");
                    builder.addInclude("admin_permission");
                    builder.addInclude("admin_role_permission");


                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }

    public static void main(String[] args) {
        MysqlGenerator.generator();
    }
}
